use log::trace;
use native_dialog::{MessageDialog, MessageType};
use xfce_bing_everyday_wallpaper::*;

async fn run(context: &Context) -> eyre::Result<()> {
    match context.action {
        Action::Normal => {
            if context.if_edit {
                let image_path = get_bing_wallpaper(context).await?;

                let image_path = image_path.to_str().unwrap();

                change_wallpaper(context, image_path)?;
            }
        }
        Action::Clean => {
            clean(context)?;
        }
    }

    Ok(())
}

#[tokio::main]
async fn main() {
    env_logger::init();

    let context = Context::from_cli().unwrap();

    loop {
        let res = run(&context).await;
        match res {
            Ok(()) => {
                break;
            }
            Err(err) => {
                let user_input = MessageDialog::new()
                    .set_type(MessageType::Error)
                    .set_title(&format!("{} Error\nWill you retry ?", PROJECT_NAME))
                    .set_text(&format!("{:?}", err))
                    .show_confirm()
                    .unwrap();
                if !user_input {
                    break;
                }
            }
        }
    }
    trace!("All correct, exited");
}
